package com.hezhi.project.module.classes.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hezhi.project.module.classes.domain.Classes;
import com.hezhi.project.module.classes.service.IClassesService;
import com.hezhi.project.module.grade.domain.Grade;
import com.hezhi.project.module.grade.service.IGradeService;
import com.hezhi.project.module.teacher.domain.Teacher;
import com.hezhi.project.module.teacher.service.ITeacherService;
import com.hezhi.framework.web.controller.BaseController;
import com.hezhi.framework.web.page.TableDataInfo;
import com.hezhi.framework.web.domain.AjaxResult;

/**
 * 班级 信息操作处理
 * 
 * @author hezhi
 * @date 2018-08-06
 */
@Controller
@RequestMapping("/module/classes")
public class ClassesController extends BaseController
{
    private String prefix = "module/classes";
	
	@Autowired
	private IClassesService classesService;
	@Autowired
	private IGradeService gradeService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequiresPermissions("module:classes:view")
	@GetMapping()
	public String classes()
	{
	    return prefix + "/classes";
	}
	
	/**
	 * 查询班级列表
	 */
	@RequiresPermissions("module:classes:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Classes classes)
	{
		startPage();
        List<Classes> list = classesService.selectClassesList(classes);
		return getDataTable(list);
	}
	
	/**
	 * 新增班级
	 */
	@RequiresPermissions("module:classes:add")
	@GetMapping("/add")
	public String add(Model model)
	{
		//查询年级列表数据
		List<Grade> listGrade = gradeService.selectGradeList(new Grade());
		model.addAttribute("listGrade", listGrade);
		//查询班主任列表
		List<Teacher> listTeacher = teacherService.selectTeacherList(new Teacher());
	    model.addAttribute("listTeacher", listTeacher);
		return prefix + "/add";
	}

	/**
	 * 修改班级
	 */
	@RequiresPermissions("module:classes:edit")
	@GetMapping("/edit/{classNumber}")
	public String edit(@PathVariable("classNumber") String classNumber, Model model)
	{
		Classes classes = classesService.selectClassesById(classNumber);
		//查询年级列表数据
		List<Grade> listGrade = gradeService.selectGradeList(new Grade());
		model.addAttribute("listGrade", listGrade);
		//查询班主任列表
		List<Teacher> listTeacher = teacherService.selectTeacherList(new Teacher());
	    model.addAttribute("listTeacher", listTeacher);
		model.addAttribute("classes", classes);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存班级
	 */
	@RequiresPermissions("module:classes:save")
	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(Classes classes)
	{
		if (classesService.saveClasses(classes) > 0)
		{
			return success();
		}
		return error();
	}
	
	/**
	 * 删除班级
	 */
	@RequiresPermissions("module:classes:remove")
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		int rows = classesService.deleteClassesByIds(ids);
		if (rows > 0)
        {
            return success();
        }
        return error();
	}
	
	/**
	 * 验证班级编码唯一
	 * @param grade
	 * @return
	 */
	@PostMapping("/checkClassNumber")
	@ResponseBody
	public String checkClassNumber(Classes classes){
		if(classes != null && !"".equals(classes.getClassNumber()) && classes.getClassNumber() != null){
			if(classesService.selectClassesById(classes.getClassNumber())!=null){
				return "1";
			}
		}
		return "0";
	}
	
	/**
	 * 选择班级树
	 * @param model
	 * @return
	 */
	@GetMapping("/selectClassesTree")
	public String selectClassesTree(Model model){
		model.addAttribute("treeName", "请选择");
		return prefix + "/tree";
	}
	
	@GetMapping("/treeData")
    @ResponseBody
    public List<Map<String, Object>> treeData()
    {
        List<Map<String, Object>> tree = classesService.selectClassesTree();
        return tree;
    }
	
}
